package cn.kgc.hotel.service.impl;

import cn.kgc.hotel.dao.RoomMapper;
import cn.kgc.hotel.dao.RoomsMapper;
import cn.kgc.hotel.entity.*;
import cn.kgc.hotel.entity.Param.RoomParam;
import cn.kgc.hotel.entity.Room;
import cn.kgc.hotel.entity.RoomType;
import cn.kgc.hotel.service.RoomService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

@Service
public class RoomServiceImpl implements RoomService {
    @Resource
    private RoomMapper roomMapper;

    @Resource
    private RoomsMapper roomsMapper;

    @Override
    public List<RoomType> searchRoomType(String[] dateList, String keyWord) {
        List<RoomType> roomTypeList = roomMapper.searchRoomType(dateList, keyWord);
        return roomTypeList;
    }

    @Override
    public RoomType searchRoomTypeById(Integer typeId, String[] dateList) {
        RoomType roomType = roomMapper.searchRoomTypeById(typeId, dateList);
        return roomType;
    }

    @Override
    public List<Room> searchRooms(Integer typeId, String[] dateList) {
        List<Room> roomList = roomMapper.searchRooms(typeId, dateList);
        return roomList;
    }


    //郅连山
    @Override
    public List<Room> showRoom(Integer roomNums, String roomType, String roomFloor, String roomState) {
        List<Room> roomList = roomsMapper.showRoom(roomNums, roomType, roomFloor, roomState);
        return roomList;
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED, isolation = Isolation.READ_COMMITTED)
    public boolean addRoom(RoomParam roomParam) {
        int i = roomsMapper.addRoom(roomParam);
        if (i > 0) {
            roomsMapper.addRoomDate(roomParam);
            return true;
        }
        return false;
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED, isolation = Isolation.READ_COMMITTED)
    public boolean updateRoom(RoomParam roomParam) {
        int i = roomsMapper.updateRoom(roomParam);
        if (i > 0) {
            roomsMapper.updateRoomDate(roomParam);
            return true;
        }
        return false;
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED, isolation = Isolation.READ_COMMITTED)
    public boolean deleteRoom(RoomParam roomParam) {
        int i = roomsMapper.deleteRoom(roomParam);
        if (i > 0) {
            roomsMapper.deleteRoomDate(roomParam);
            return true;
        }
        return false;
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED, isolation = Isolation.READ_COMMITTED)
    public boolean roomReserve(RoomReserve roomReserve, List<RoomDate> roomDateList,
                               List<RoomCustomer> roomCustomerList, List<Customer> customerList) {
        int addRoomReserve = roomMapper.addRoomReserve(roomReserve);
        int addRoomDate = roomMapper.addRoomDate(roomDateList);
        int addRoomCustomer = roomMapper.addRoomCustomer(roomCustomerList);
        int addCustomer = roomMapper.addCustomer(customerList);
        if (addRoomReserve > 0 && addRoomDate > 0 && addRoomCustomer > 0 && addCustomer > 0) {
            return true;
        }
        return false;
    }

    @Override
    public List<RoomReserve> searchRoomReserve(int userId) {
        List<RoomReserve> roomReserveList = roomMapper.searchRoomReserve(userId);
        return roomReserveList;

    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED, isolation = Isolation.READ_COMMITTED)
    public boolean updateRoomReserve(RoomReserve roomReserve) {
        int addRoomReserve = roomMapper.addRoomReserve(roomReserve);
        List<Customer> customerList = new ArrayList<>();
        for (RoomCustomer roomCustomer : roomReserve.getRoomCustomerList()) {
            customerList.add(roomCustomer.getCustomer());

        }
        for (Customer customer : customerList) {
            System.out.println("BBBBBBBBBBBBBBBBBBBBBBBBBB" + customer.getName());
        }

        int updateCustomer = roomMapper.updateCustomer(customerList);
        if (addRoomReserve > 0 && updateCustomer > 0) {
            return true;
        }
        return false;
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED, isolation = Isolation.READ_COMMITTED)
    public boolean cancelRoomReserve(RoomReserve roomReserve) {
        int addRoomReserve = roomMapper.addRoomReserve(roomReserve);
        int updateRoomDate = roomMapper.deleteRoomDate(roomReserve);
        if (addRoomReserve > 0 && updateRoomDate > 0) {
            return true;
        }
        return false;
    }


}
